﻿' Copyright (C) 2011  Jan-Willem de Bruyn
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Imports System.IO
Imports System.Net
Imports System.Text
Imports WebApplication3.Site
Imports psylib_2._0.psyftp


Public Class transfer

    Inherits System.Web.UI.Page
    Dim mywebclient As New WebClient()
    Dim pagehtmlbytes() As Byte
    Dim outf8 As UTF8Encoding = New UTF8Encoding()
    Dim item As String
    Dim usr As String
    Dim pass As String


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Public Sub open()



    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        pagehtmlbytes = mywebclient.DownloadData(TextBox1.Text)
        TextBox2.Text = outf8.GetString(pagehtmlbytes).ToString

    End Sub

    Protected Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
        Dim readobj As New System.IO.StreamReader(file1.PostedFile.InputStream)
        TextBox2.Text = readobj.ReadToEnd()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        usr = TextBox3.Text
        pass = TextBox4.Text
        Dim txtbox As String = TextBox5.Text
        Dim path As String = TextBox7.Text
        If Not path.EndsWith("/") Then
            path = path & "/"
        End If
        If Not txtbox.StartsWith("ftp://") Then
            txtbox = "ftp://" & txtbox
        End If
        If Not path.StartsWith("/") Then
            path = "/" & path
        End If
        Dim stream As Stream = New MemoryStream(ASCIIEncoding.Default.GetBytes(TextBox2.Text))
        upload_ftp(txtbox, usr, pass, path, stream, TextBox6.Text)
        'client.UploadData("ftp://" & TextBox3.Text & ":" & TextBox4.Text & "@" & TextBox5.Text & ":21" & "/" & TextBox6.Text, buffer)

    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
        listdir()
    End Sub
    Public Sub listdir()
        usr = TextBox3.Text
        pass = TextBox4.Text
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        Dim txtbox As String = TextBox5.Text
        Dim path As String = TextBox7.Text
        If Not path.EndsWith("/") Then
            path = path & "/"
        End If
        If Not txtbox.StartsWith("ftp://") Then
            txtbox = "ftp://" & txtbox
        End If
        If Not path.StartsWith("/") Then
            path = "/" & path
        End If
        If Not path.EndsWith("/") Then
            path = path & "/"
        End If
        Dim result As Object = list_ftp(txtbox, usr, pass, path)
        Dim arr As ArrayList
        If TypeOf result Is ArrayList Then
            arr = result
        Else
            arr = Nothing
            MsgBox(result.ToString)
            Exit Sub
        End If

        Dim item As ListItem = Nothing

        For Each strnas As String In arr

            If strnas Like "*.php" Or strnas Like "*.html" Or strnas Like "*.txt" Or strnas Like "*.shtml" Or strnas Like "*.xhtml" Then
                ListBox2.Items.Add(strnas)
            Else
                ListBox1.Items.Add(strnas)
            End If


        Next

    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
        usr = TextBox3.Text
        pass = TextBox4.Text

        TextBox7.Text = TextBox7.Text & ListBox1.SelectedItem.ToString & "/"
        DropDownList2.Items.Add("/" & ListBox1.SelectedItem.ToString & "/")
            listdir()

    End Sub
    Public Sub editfile()
        usr = TextBox3.Text
        pass = TextBox4.Text
        
        Dim txtbox As String = TextBox5.Text

        Dim path As String = TextBox7.Text
        If Not path.EndsWith("/") Then
            path = path & "/"
        End If
        If Not txtbox.StartsWith("ftp://") Then
            txtbox = "ftp://" & txtbox
        End If
        If Not path.StartsWith("/") Then
            path = "/" & path
        End If
        If Not path.EndsWith("/") Then
            path = path & "/"
        End If

        Dim sr As New StreamReader(CType(download_ftp(txtbox, usr, pass, path, item), Stream))
        Dim str As String = sr.ReadToEnd
        TextBox2.Text = str

        sr.Close()
        sr = Nothing
        usr = ""
        pass = ""
        tagsadd()
    End Sub

    Public Sub tagsadd()
        If TextBox7.Text.ToString.EndsWith(".shtml") Then
            DropDownList1.Items.Add("<!--#include file=VALUE -->")
        End If
    End Sub
    Protected Sub addsnip_Click(ByVal sender As Object, ByVal e As EventArgs) Handles addsnip.Click
        DropDownList1.Items.Add(TextBox8.Text)
    End Sub



    Protected Sub goto_Click(ByVal sender As Object, ByVal e As EventArgs) Handles [goto].Click
        TextBox7.Text = DropDownList2.SelectedItem.ToString
        listdir()
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox2.TextChanged

    End Sub

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
        item = ListBox1.SelectedItem.ToString

    End Sub

    Protected Sub TextBox4_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox4.PreRender
        TextBox4.Attributes("value") = TextBox4.Text
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click
        TextBox6.Text = ListBox2.SelectedItem.ToString
        item = ListBox2.SelectedItem.ToString
        editfile()
    End Sub
End Class